package com.oasistudio.ads.expert;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.telephony.TelephonyManager;

public class ServerAccessor {
	
	private Activity context;
	private TelephonyManager telephonyManager;
	public ServerAccessor(Activity context){
		//System.out.println("ServerAccessor.ServerAccessor()");
		this.context = context;
		this.telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
	}
	
    public Location getLocation() {  
        LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);  
        //System.out.println("locationManager = "+locationManager);
        Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);  
        //System.out.println("LocationManager.GPS_PROVIDER location = "+location);
        //for(String provider : locationManager.getAllProviders()){
        //	System.out.println("provider = "+provider);
        //}
        if (location == null) {  
            location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);  
            System.out.println("LocationManager.NETWORK_PROVIDER location = "+location);
        }
        return location;  
    }  	

	public void click(String union) {
		String href = context.getResources().getString(R.string.href_click);
		List<NameValuePair> nameValuePairs = getClientInfo();
        nameValuePairs.add(new BasicNameValuePair("unionName", union));
        nameValuePairs.add(new BasicNameValuePair("clientTimeMillis", ""+System.currentTimeMillis()));
		access(href, nameValuePairs);
	}

	public void fail(String union) {
		String href = context.getResources().getString(R.string.href_fail);
		List<NameValuePair> nameValuePairs = getClientInfo();
        nameValuePairs.add(new BasicNameValuePair("unionName", union));
		access(href, nameValuePairs);
	}

	public void impression(String union, long time) {
		//System.out.println("ServerAccessor.impression() union ="+union+" time="+time);
		String href = context.getResources().getString(R.string.href_impression);
		//System.out.println("ServerAccessor.impression() href="+href);
		List<NameValuePair> nameValuePairs = getClientInfo();
        nameValuePairs.add(new BasicNameValuePair("unionName", union));
        nameValuePairs.add(new BasicNameValuePair("responseTime", ""+time));
		access(href, nameValuePairs);
	}
	
	private List<NameValuePair> getClientInfo(){
	        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
	        nameValuePairs.add(new BasicNameValuePair("deviceId", telephonyManager.getDeviceId()));
	        nameValuePairs.add(new BasicNameValuePair("line1Number", telephonyManager.getLine1Number()));
	        nameValuePairs.add(new BasicNameValuePair("networkOperatorName", telephonyManager.getNetworkOperatorName()));
	        Location location = this.getLocation();
	        if(location!=null){
		        nameValuePairs.add(new BasicNameValuePair("latitude", ""+location.getLatitude()));
		        nameValuePairs.add(new BasicNameValuePair("longitude", ""+location.getLongitude()));
	        }
	        nameValuePairs.add(new BasicNameValuePair("networkType", telephonyManager.getNetworkType()+""));
	        nameValuePairs.add(new BasicNameValuePair("phoneType", ""+telephonyManager.getPhoneType()));
	        nameValuePairs.add(new BasicNameValuePair("simOperatorName", telephonyManager.getSimOperatorName()));
	        return nameValuePairs;
	}
	private void access(String href, List<NameValuePair> nameValuePairs) {
		HttpClient httpclient = new DefaultHttpClient();
	    HttpPost httppost = new HttpPost(href);
	    //System.out.println("ServerAccessor.access() href = "+href);
	    try {
	        // Add your data
	        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
	        // Execute HTTP Post Request
	        //HttpResponse response = 
	        httpclient.execute(httppost);
	        //System.out.println("response.getAllHeaders().length = "+response.getAllHeaders().length);
	    } catch (ClientProtocolException e) {
	        // TODO Auto-generated catch block
	    	e.printStackTrace();
	    } catch (IOException e) {
	        // TODO Auto-generated catch block
	    	e.printStackTrace();
	    }
	}
}
